$m(function(args){
	//应用ID
	var analyseId = args.analyseId;
	//流程ID
	var id = args.resourceId;
	//流程内容
	var $content = args.content;
	
	var $callback = args.callback;
	
	var $mainDialog = $('#tempo-mine-snapshot-add-layout');
	var renderDialog = function(){
		$mainDialog.dialog({
			title: '保存版本',
			autoOpen : true,
			width : 450,
			height : 280,
			resizable:false,
			modal : true,
			close:function(){
				$mainDialog.dialog('destroy').remove();
			},
			open:function(){
				renderPage();
			},
			buttons:[{
				type: 'ok', 
				click:save
			},{ 
				type: 'cancel', 
				click: function(){
					$mainDialog.dialog('close');
				} 
			}]
		});
	}
	
	
	var save = function(){
		var form = $mainDialog.find("form");
		if(!form.formValidate()){
			return; 
		}  
		
		var process = $("<div/>").tempoprogressbar();
		var params = buildParams();
		
		$.ajax({
			url: contextPath + '/api/ai/snapshot/save',
			data: params,
			type:'post',
			success: function(resultData){
				process.tempoprogressbar('destroy');
				if(resultData.success){
					$mainDialog.dialog('close');
					if($callback){
						$callback.call(this, resultData.data);
					}
				}else{
					$("<div/>").alert({
						msg: resultData.message
					});
				}
			},
			error:function(){
				process.tempoprogressbar('destroy');
			}
		});
	
	}
	
	
	
	
	var buildParams = function(){
		return {
			name: $mainDialog.find('input[name="name"]').val(),
			remark: $mainDialog.find('textarea[name="description"]').val(),
			id: id,
			analyseId: analyseId,
			flow: $content,
			version: $mainDialog.find('input[name="version"]').val() 
		};
	}
	
	
	
	var renderPage = function(){
		var process = $("<div/>").tempoprogressbar();
		$.ajax({
			url:contextPath + '/api/ai/snapshot/getDefaultName',
			type:'post',
			data: {
				id: id,
				analyseId: analyseId
			},
			success:function(resultData){
				process.tempoprogressbar('destroy');
				var data = resultData.data;
				var targetInput = $mainDialog.find('input[name="name"]');
				targetInput.val(data.name).validatebox({
					tip: TempoUtils.locale("bi.snapshot_input_valid_tip"),
					required: true,
					maxLength: 60,
					autofill: false
				});
				$mainDialog.find('label[name="version"]').text('V' + data.version);
				$mainDialog.find('input[name="version"]').val(data.version);
				$mainDialog.find('textarea[name="description"]').validatebox({
					required: false,
					maxLength: 100,
					autofill: false,
					valids: [/^[a-zA-Z0-9_,.?，。？！~（）()【】《》{}:;""''：“”、·|&*$￥#@……^+=/\u4e00-\u9fa5]+$/],
					validTip: TempoUtils.locale("mine.snapshotdesc")
				});
			},
			error:function(){
				process.tempoprogressbar('destroy');
			}
		});
	}
	
	renderDialog();
});